﻿Public Class ExcelExportForm

    Private Sub LoadProviderFilter()

        Dim sql As String

        sql = "select * from View_Provider_Filter_Options"
        ProviderFilter.zDataView = LoadDataView(sql)

    End Sub

    Private Sub LoadAllocationFilter()

        Dim sql As String

        sql = "select a.Allocation_ID as [Allocation ID], a.Allocation_Description as [Allocation Description], b.Budget_ID as [Budget ID], b.Budget_Description as [Budget Description] from Allocation_Lines as a inner join Budgets as b on a.Budget_ID = b.Budget_ID"
        AllocationFilter.zDataView = LoadDataView(sql)

    End Sub


    Private Sub RunExport()

        Dim fromdate As String
        fromdate = "1 " & FromComboBox.Text & " " & FromNumericUpDown.Value

        Dim todate As String
        todate = "1 " & ToComboBox.Text & " " & ToNumericUpDown.Value

        Dim RIDList As New List(Of String)
        Dim r As DataRowView

        RIDList.Clear()

        For Each r In ProviderFilter.zDataView
            RIDList.Add("''" & r("RID").ToString & "''")
        Next

        Dim RidString As String

        RidString = String.Join(",", RIDList.ToArray)


        Dim AllocList As New List(Of String)
        Dim a As DataRowView

        AllocList.Clear()
        For Each a In AllocationFilter.zDataView
            AllocList.Add("''" & a("Allocation ID").ToString & "''")
        Next

        Dim AllocString As String

        AllocString = String.Join(",", AllocList.ToArray)


        Dim ShowTots As Integer

        If ShowProvTotsCheckBox.Checked = True Then
            ShowTots = 1
        Else
            ShowTots = 0
        End If

        Dim p As New List(Of Object)
        p.Clear()
        p.Add(fromdate)
        p.Add(todate)
        p.Add(RidString)
        p.Add(AllocString)
        p.Add(ShowTots)
        DoSomeWork("ExcelPaymentsReport", p)


    End Sub


    Private Sub ExcelExportForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProviderFilter.zShowButtons = False
        ProviderFilter.zInfolabelOverride = "Provider Filter"
        AllocationFilter.zShowButtons = False
        AllocationFilter.zInfolabelOverride = "Allocations Filter"

        LoadProviderFilter()
        LoadAllocationFilter()

        FromNumericUpDown.Value = Year(Now)
        ToNumericUpDown.Value = Year(Now) + 1

        FromComboBox.SelectedIndex = 7
        ToComboBox.SelectedIndex = 6

    End Sub

    Private Sub RunExportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunExportButton.Click



        ProviderFilter.ApplyFilter()
        AllocationFilter.ApplyFilter()

        If AllocationFilter.zRecordCount = 0 Then
            MessageBox.Show("Please remove or make a definite selection for the Allocation filter", "Invalid Allocation Selection", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        RunExport()

        ProviderFilter.ClearFilter()
        AllocationFilter.ClearFilter()


    End Sub

    Private Sub FromNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FromNumericUpDown.ValueChanged
        If FromNumericUpDown.Value > ToNumericUpDown.Value Then
            ToNumericUpDown.Value = FromNumericUpDown.Value
        End If

        If FromNumericUpDown.Value = ToNumericUpDown.Value Then

            If FromComboBox.SelectedIndex > ToComboBox.SelectedIndex Then
                ToComboBox.SelectedIndex = FromComboBox.SelectedIndex

            End If

        End If
    End Sub

    Private Sub ToNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToNumericUpDown.ValueChanged
        If FromNumericUpDown.Value > ToNumericUpDown.Value Then
            FromNumericUpDown.Value = ToNumericUpDown.Value
        End If
    End Sub

    Private Sub FromComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FromComboBox.SelectedIndexChanged
        If FromNumericUpDown.Value = ToNumericUpDown.Value Then

            If FromComboBox.SelectedIndex > ToComboBox.SelectedIndex Then
                ToComboBox.SelectedIndex = FromComboBox.SelectedIndex

            End If

        End If

        If FromNumericUpDown.Value = ToNumericUpDown.Value Then

            If FromComboBox.SelectedIndex > ToComboBox.SelectedIndex Then
                FromComboBox.SelectedIndex = ToComboBox.SelectedIndex

            End If

        End If
    End Sub

    Private Sub ToComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToComboBox.SelectedIndexChanged
        If FromNumericUpDown.Value = ToNumericUpDown.Value Then

            If FromComboBox.SelectedIndex > ToComboBox.SelectedIndex Then
                FromComboBox.SelectedIndex = ToComboBox.SelectedIndex

            End If

        End If
    End Sub

    Private Sub ExcelExportForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        ReportsForm.Close()
    End Sub
End Class